import { computed } from 'vue'
import { useI18n } from 'vue-i18n'
import { LocaleMapping } from '@/components/i18n/index.js'

export default function useLocale() {
  const i18n = useI18n()
  const currentLocale = computed(() => {
    return i18n.locale.value
  })

  const changeLocale = (value: 'zh-CN' | 'en-US') => {
    if (i18n.locale.value === value)
      return
    if (LocaleMapping[value]) {
      i18n.locale.value = LocaleMapping[value]
      localStorage.setItem('lang', value)
    }
    else {
      i18n.locale.value = LocaleMapping['zh-CN']
      localStorage.setItem('lang', 'zh-CN')
    }
  }
  return {
    currentLocale,
    changeLocale,
  }
}
